﻿using UnityEngine;
using System.Collections;

public abstract class MenuBase : SparkBehaviour
{
    /// <summary>
    /// 按钮的声音
    /// </summary>
    public string[] soundName;
    public string BGMName;
    protected void Start()
    {
        UIButton[] buttons = GetComponentsInChildren<UIButton>();
        int index = 0;
        foreach (UIButton btn in buttons)
        {  
            UIEventListener listener = UIEventListener.Get(btn.gameObject);
            listener.onClick += OnClick;
            listener.onPress += OnKeyDown;
            listener.onDrag += OnDrag;
            listener.onDragEnd += OnDragEnd;
            if (btn.gameObject.GetComponent<UIPlaySound>() == null)
            {
                UIPlaySound sound = btn.gameObject.AddComponent<UIPlaySound>();
                sound.audioClip = Resources.Load("Client/Music/" + soundName[index]) as AudioClip;
            }
            index++;
        }
        UIPlaySound[] sounds = gameObject.GetComponentsInChildren<UIPlaySound>();
        foreach (UIPlaySound sound in sounds)
        {
//            sound.volume = GameManager.Instance.soundManager.initSound;
        }
        //UIPanel panel = GetComponent<UIPanel>();
//        SQManager.Instance.soundManager.playBGM(BGMName, true);
        onStart();
    }
  
    public virtual void onStart()
    {
        //Debug.Log("menu started.");
    }

    public virtual void OnSelectionChange(GameObject btn, bool state)
    {
        
    }

    public virtual void OnKeyDown(GameObject btn, bool state)
    {
    }

    public virtual void OnClick(GameObject btn)
    {
        //Debug.Log("Clicked.");
    }
    public virtual void OnDrag(GameObject btn, Vector2 delta)
    {
    }
    public virtual void OnDragEnd(GameObject btn)
    { }

    public virtual GameObject popMenu(string name)
    {
       return UIManager.Instance.popMenu(name);
    }

    public virtual void popNextMenu(string name)
    {
        unpopMe();
        UIManager.Instance.popMenu(name);
    }
    
    public virtual void unpopMe()
    {
        UIManager.Instance.removeAndDestroy(this);
    }

    /// <summary>
    /// 把以秒为单位的时间转换成标准的时间显示形式 -> 00:00:00 (时：分：秒)
    /// </summary>
    /// <param name="second"></param>
    /// <returns></returns>
    protected string ConvertTimeToString(int second)
    {
        string subtime;
        string time;
        int remainSecond = second;
        if (second < 0)
        {
            return null;
        }
        int hours = remainSecond / 3600;
        remainSecond = remainSecond % 3600;
        subtime = hours < 10 ? "0" + hours.ToString() : hours.ToString();
        time = subtime + ":";

        int minutes = remainSecond / 60;
        remainSecond = remainSecond % 60;
        subtime = minutes < 10 ? "0" + minutes.ToString() : minutes.ToString();
        time = time + subtime + ":";

        subtime = remainSecond < 10 ? "0" + remainSecond.ToString() : remainSecond.ToString();
        time = time + subtime;
        return time;
    }
    /// <summary>
    /// 把以豪秒为单位的时间转换成标准的时间显示形式 -> 00:00:00 (分：秒：毫秒)
    /// </summary>
    /// <param name="msec"></param>
    /// <returns></returns>
    protected string ConvertTimeToStringHMMs(int msec)
    {
        string subtime;
        string time;
        int remainSecond = msec/1000;
        if (msec < 0)
        {
            return null;
        }
        //分
        int minutes = remainSecond / 60;
        remainSecond = remainSecond % 60;
        subtime = minutes < 10 ? "0" + minutes.ToString() : minutes.ToString();
        time = subtime + ":";
        //秒
        subtime = remainSecond < 10 ? "0" + remainSecond.ToString() : remainSecond.ToString();
        time = time + subtime +":";
        //毫秒
        int ms = (msec % 1000)/10;
        subtime = ms < 10 ? "0" + ms.ToString() : ms.ToString();
        time = time + subtime;
        return time;
    }
    /// <summary>
    /// 对具有UI2DSprite组件的物体换图片
    /// </summary>
    /// <param name="objPath">基于根节点的游戏物体路径</param>
    /// <param name="imgPath">放在Resource目录下的图片路径</param>
    protected void SetImgForObj(string objPath, string imgPath)
    {
        Sprite sprite = Resources.Load(imgPath, typeof(Sprite)) as Sprite;
        if (sprite == null)
        {
            Debug.Log("no img named :" + imgPath);
            return;
        }
        UI2DSprite objImg = cachedTransform.FindChild(objPath).GetComponent<UI2DSprite>();
        objImg.nextSprite = sprite;
    }
    protected void SetImgForObj(Transform target, string imgPath)
    {
        UI2DSprite objImg = target.GetComponent<UI2DSprite>();
        Sprite sprite = Resources.Load(imgPath, typeof(Sprite)) as Sprite;
        target.GetComponent<UIWidget>().width = sprite.texture.width;
        target.GetComponent<UIWidget>().height = sprite.texture.height;
        target.GetComponent<UIWidget>().depth = 2000;
        if (sprite == null)
        {
            Debug.Log("no img named :" + imgPath);
            return;
        }
        objImg.nextSprite = sprite;
    }
    /// <summary>
    /// 对具有UILabel组件的物体设置显示的文本内容
    /// </summary>
    /// <param name="objPath">基于根节点的游戏物体路径</param>
    /// <param name="text">需要显示的文本内容</param>
    protected void SetTextForObj(string objPath, string text)
    {
        cachedTransform.FindChild(objPath).GetComponent<UILabel>().text = text;
    }
    protected void SetTextForObj(Transform target, string text)
    {
        target.GetComponent<UILabel>().text = text;
    }
}

